var SystemUtil = {
  getUser: function () {
    var user = window.sessionStorage.getItem('user');
    if (user !== undefined && user !== null) {
      return new Promise(function (resole) {
        user = JSON.parse(user);
        resole(user);
      });
    }

    return ajaxRequest('user/getUserName').then(function (user) {
      var permissionList = user.permissionList;
      permissionList = _.map(permissionList, 'url');
      permissionList = _.filter(permissionList, function (permission) {
        return !permission.endsWith('.html') && !permission.endsWith('.jsp');
      });

      var pageList = user.permissionList;
      pageList = _.map(pageList, 'url');
      pageList = _.filter(pageList, function (page) {
        return page.endsWith('.html') || page.endsWith('.jsp');
      });

      user.pageList = pageList;
      user.permissionList = permissionList;

      var fixedWarehouseCreateUser = user.userId === '3260';
      user.userPagePermission = {
        fixedWarehouseCreateUser: fixedWarehouseCreateUser
      }
      window.sessionStorage.setItem('user', JSON.stringify(user));
      return user;
    });
  },

  getLoginUser: function () {
    return ajaxRequest('user/getUserName').then(function (user) {
      var permissionList = user.permissionList;
      permissionList = _.map(permissionList, 'url');
      permissionList = _.filter(permissionList, function (permission) {
        return !permission.endsWith('.html') && !permission.endsWith('.jsp');
      });

      var pageList = user.permissionList;
      pageList = _.map(pageList, 'url');
      pageList = _.filter(pageList, function (page) {
        return page.endsWith('.html') || page.endsWith('.jsp');
      });

      user.pageList = pageList;
      user.permissionList = permissionList;

      var fixedWarehouseCreateUser = user.userId === '3260';
      user.userPagePermission = {
        fixedWarehouseCreateUser: fixedWarehouseCreateUser
      }
      window.sessionStorage.setItem('user', JSON.stringify(user));
      return user;
    });
  },

  isEmptyObject:function(obj){
    for (var key in obj) {
      return true;
    }
    return false;
  }
};

var filterInput = {
  /**
   * 后台传输数据仓库类型必须为 type  属性与输入框的id对应  dataList id为输入框id+List
   * typeValue 仓库类型,没有传 ''
   * idArray 为属性集合，对应输入框id,配置
   * dataArray 数据集合
   */
  filterView: function (typeValue, idArry, dataArray) {
    var filterArray = new Array(); //过滤后的数据数组
    for (var i = 0; i < dataArray.length; i++) {
      var bool = true;
      /*先比对仓库，仓库不对直接跳过*/
      if (typeValue != '' && dataArray[i]['type'] != typeValue) {
        continue;
      }
      //循环要筛选的属性
      for (var j = 0; j < idArry.length; j++) {
        var inputValue = $("#" + idArry[j]).val();
        //输入的值不匹配
        if (inputValue != '' && dataArray[i][idArry[j]].indexOf(inputValue) == -1) {
          bool = false;
          break;
        }
      }
      if (bool) {
        filterArray.push(dataArray[i]);
      }
    }
    //filterArray 以上得到过滤后的数据数组 
    for (var i = 0; i < idArry.length; i++) {
      var listArray = new Array();
      //去重
      listArray = _.uniqBy(filterArray, idArry[i]);
      listArray = _.map(listArray, idArry[i]);
      //渲染
      var listId = "#" + idArry[i] + "List";
      $(listId).html("");
      for (var j = 0; j < listArray.length; j++) {
        $(listId).append("<option value='" + listArray[j] + "'>");
      }
    }

  }
}

var tableUtil = {
  download: function (url) {
    window.open(url, "_blank");
  },
  dataTablesStaticUtil: function (id, columnIndex, scrollX) {
    if (scrollX === undefined) {
      scrollX = true;
    }
    $('#' + id).dataTable().fnDestroy();
    table = $('#' + id).DataTable({
      //		 "searching": true,//屏蔽datatales的查询框
      "scrollX": scrollX,
      //		 "iLeftColumns":2 ,
      "fixedColumns": { //设置固定列数
        leftColumns: columnIndex
      },
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        "sSearch": "搜索：",
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }
    });
  },
  dataTablesStaticUtilNoSearch: function (id, scrollX) {
    if (scrollX === undefined) {
      scrollX = true;
    }
    $('#' + id).dataTable().fnDestroy();
    table = $('#' + id).DataTable({
      "processing": false, //打开数据加载时的等待效果
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //屏蔽datatales的查询框
      "aLengthMenu": [20], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
      "scrollX": scrollX,
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }
    });
  }
};

var ajaxUtil = {
  //必填：url为请求路径，data为参数，successRollBac为请求成功后处理数据方法
  //选填：type为请求格式,默认post（get、post）   dataType为数据类型，默认json
  ajaxRuquest: function (url, resData, successRollBack, errorRollBack) {
    var type = type || "post";
    var dataType = dataType || "json";
    var index = layer.load(1, {
      shade: [0.1, '#fff']
    });
    $.ajax({
      url: url,
      type: type,
      data: resData,
      dataType: dataType,
      success: function (data, status, xhr) {
        layer.close(index);
        if (xhr.getResponseHeader("permissionStatus") == 'noPermission') {
          layAlert("没有权限");
          return false;
        }
        if (data.result) {
          data = data.data;
          successRollBack(data);
        } else {
          layAlert(data.info);
          if (errorRollBack !== undefined) {
            errorRollBack(data.info);
          }
        }
      }
    });
  },
  ajaxRuquestNoQuan: function (url, resData, successRollBack) {
    var type = type || "post";
    var dataType = dataType || "json";
    $.ajax({
      url: url,
      type: type,
      data: resData,
      dataType: dataType,
      success: function (data, status, xhr) {
        if (xhr.getResponseHeader("permissionStatus") == 'noPermission') {
          layAlert("没有权限");
          return false;
        }
        if (data.result) {
          data = data.data;
          successRollBack(data);
        } else {
          if (data.info == '用户失效') {
            location.href = 'login.html';
          } else {
            layAlert(data.info);
          }
        }
      }
    });
  },
  //同步请求
  ajaxAsyncRuquest: function (url, resData, successRollBack) {
    var type = type || "post";
    var dataType = dataType || "json";
    var index = layer.load(1, {
      shade: [0.1, '#fff']
    });
    $.ajax({
      url: url,
      type: type,
      data: resData,
      dataType: dataType,
      async: false, //表示同步
      success: function (data, status, xhr) {
        layer.close(index);
        if (xhr.getResponseHeader("permissionStatus") == 'noPermission') {
          layAlert("没有权限");
          return false;
        }
        if (data.result) {
          data = data.data;
          successRollBack(data);
        } else {
          layAlert(data.info);
        }
      }
    });
  },
  fileAjax: function (url, fileId, successRollBack, errorRollBack) {
    $.ajaxFileUpload({
      url: url,
      type: 'POST',
      secureuri: false,
      fileElementId: fileId,
      dataType: 'text',
      success: function (data) {
        var start = data.indexOf(">{");
        var end = data.indexOf("}<");
        var jsonStr = data.substring(start + 1, end + 1);
        data = eval("(" + jsonStr + ")");
        //		         return (jsonStr instanceof Object)?jsonStr:eval("(" + jsonStr + ")"); 
        if (data.result) {
          data = data.data;
          successRollBack(data);
        } else {
          layAlert(data.info);
          if (typeof errorRollBack == "function") {
            errorRollBack(data);
          }

        }
      },
      error: function (data, status, e) { //提交失败自动执行的处理函数。
        //				console.log(data);
        //	            console.log(e);
      }
    });
  },

  /**
   * 打印异常信息,但暂时没法解决捕获异常
   * 李斌
   * @param id
   * @param count
   * @param url
   * @param sendData
   * @param columns
   * @param colIndexArray
   * @param columnFunction
   * @returns
   */
  dataTablesAjaxPrintException: function (id, count, url, sendData, columns, colIndexArray, columnFunction) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "ajax": {
        "url": url,
        "dataSrc": "returnData",
        "data": sendData,
        "error": function (xhr, textStatus, error) {
          console.log(xhr);
          console.log(textStatus);
          console.log(error);
          $('#' + id).dataTable().fnProcessingIndicator();
        }
      },
      "columns": columns,
      "columnDefs": [{
        "targets": colIndexArray,
        "createdCell": function (td, cellData, rowData, row, col) {
          columnFunction(td, cellData, rowData, row, col);
        }
      }],
      "processing": false, //打开数据加载时的等待效果
      "serverSide": true, //打开后台分页
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //屏蔽datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
      "responsive": true, //自适应
      "fixedHeader": false, //表头固定
      "scrollX": true,
      "autoWidth": true,
      fixedColumns: {
        leftColumns: 4,
        bScrollCollapse: true
      },
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        //	              "sSearch": "搜索：",  
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }

    });
    //后台抛出异常后,处理
    table.on('xhr.dt', function (e, settings, json, xhr) {
      if (xhr !== undefined) {
        if (xhr.status == 200) {
          //自己的代码
          var json = xhr.responseText;
          var jsonString = JSON.parse(json);
          if (jsonString.result === false) {
            layAlert(jsonString.info);
            return;
          }
        }
      }
    });
    return table;


    //	  $("#"+id).DataTable().columns.adjust();
    //	  $.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust();
  },
  /**
   * 行创建后带回调的datatables
   * 李斌
   */
  dataTablesAjaxAndInitRowFunction: function (id, count, url, sendData, columns, colIndexArray, columnFunction, rowFunction) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "ajax": {
        "url": url,
        "dataSrc": "returnData",
        "data": sendData,
        "error": function (xhr, textStatus, error) {
          console.log(xhr);
          console.log(textStatus);
          console.log(error);
          $('#' + id).dataTable().fnProcessingIndicator();
        }
      },
      "columns": columns,
      "columnDefs": [{
        "targets": colIndexArray,
        "createdCell": function (td, cellData, rowData, row, col) {
          columnFunction(td, cellData, rowData, row, col);
        }
      }],
      "processing": false, //打开数据加载时的等待效果
      "serverSide": true, //打开后台分页
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //屏蔽datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
      "responsive": true, //自适应
      "fixedHeader": true, //表头固定
      //	           "scrollX":true,
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        //	              "sSearch": "搜索：",  
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      },
      //行创建后的回调
      "createdRow": function (row, data, dataIndex) {
        rowFunction(row, data, dataIndex);
      }
    });
    return table;


    //	  $("#"+id).DataTable().columns.adjust();
    //	  $.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust();
  },
  /**
   * 不固定标题的dataTables
   * libin
   */
  dataTablesAjaxNotFxiaction: function (id, count, url, sendData, columns, colIndexArray, columnFunction, rowFunction) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "ajax": {
        "url": url,
        "dataSrc": "returnData",
        "data": sendData,
        "error": function (xhr, textStatus, error) {
          console.log(xhr);
          console.log(textStatus);
          console.log(error);
          $('#' + id).dataTable().fnProcessingIndicator();
        }
      },
      "columns": columns,
      "columnDefs": [{
        "targets": colIndexArray,
        "createdCell": function (td, cellData, rowData, row, col) {
          columnFunction(td, cellData, rowData, row, col);
        }
      }],
      "processing": false, //打开数据加载时的等待效果
      "serverSide": true, //打开后台分页
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //屏蔽datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
      "responsive": true, //自适应
      "fixedHeader": true, //表头固定
      //           "scrollX":true,
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        //              "sSearch": "搜索：",  
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }
    });
    return table;


    //  $("#"+id).DataTable().columns.adjust();
    //  $.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust();
  },


  dataTablesAjax: function (id, count, url, sendData, columns, colIndexArray, columnFunction) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "ajax": {
        "url": url,
        "dataSrc": "returnData",
        "data": sendData,
        "error": function (xhr, textStatus, error) {
          console.log(xhr);
          console.log(textStatus);
          console.log(error);
          if ($('#' + id).dataTable().fnProcessingIndicator !== undefined) {
            $('#' + id).dataTable().fnProcessingIndicator();
          }

        }
      },
      "columns": columns,
      "columnDefs": [{
        "targets": colIndexArray,
        "createdCell": function (td, cellData, rowData, row, col) {
          columnFunction(td, cellData, rowData, row, col);
        }
      }],
      "processing": false, //打开数据加载时的等待效果
      "serverSide": true, //打开后台分页
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //是否显示datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //是否显示tables的一页展示多少条记录的下拉列表
      "responsive": true, //自适应
      "fixedHeader": true, //表头固定
      //		           "scrollX":true,
      "scrollY": 500,
      "scrollCollapse": true,
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        //		              "sSearch": "搜索：",  
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }
    });
    return table;


    //		  $("#"+id).DataTable().columns.adjust();
    //		  $.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust();
  },
  /**
   * 带自定义columnDefs的datatables
   * @Author 王荣
   */
  dataTablesAjaxWithcolumnDefs: function (id, count, url, sendData, columns, columnDefs) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "ajax": {
        "url": url,
        "dataSrc": "returnData",
        "data": sendData,
        "error": function (xhr, textStatus, error) {
          console.log(xhr);
          console.log(textStatus);
          console.log(error);
          if ($('#' + id).dataTable().fnProcessingIndicator !== undefined) {
            $('#' + id).dataTable().fnProcessingIndicator();
          }

        }
      },
      "columns": columns,
      "columnDefs": columnDefs,
      "processing": false, //打开数据加载时的等待效果
      "serverSide": true, //打开后台分页
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //是否显示datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //是否显示tables的一页展示多少条记录的下拉列表
      "responsive": true, //自适应
      "fixedHeader": true, //表头固定
      "scrollY": 500,
      "scrollCollapse": true,
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }
    });
    return table;
  },
  /**
   * js数据源
   * @Author 王荣
   */
  dataTablesJavascriptSourcedData: function (id, count, dataSet, columns, columnDefs) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "data": dataSet,
      "columns": columns,
      "columnDefs": columnDefs,
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": false, //是否显示datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //是否显示tables的一页展示多少条记录的下拉列表
      "responsive": true, //是否自适应
      "fixedHeader": true, //表头是否固定
      "scrollY": 500,
      "scrollCollapse": true,
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      }
    });
    return table;
  },
  /**
   * 带列筛选功能的datatables
   * @Author 王荣
   */
  dataTablesFilterAjax: function (id, count, url, sendData, columns, colIndexArray, columnFunction) {
    $('#' + id).dataTable().fnDestroy();
    var table = $('#' + id).DataTable({
      "ajax": {
        "url": url,
        "dataSrc": "returnData",
        "data": sendData,
        "error": function (xhr, textStatus, error) {
          console.log(xhr);
          console.log(textStatus);
          console.log(error);
          $('#' + id).dataTable().fnProcessingIndicator(false);
        }
      },
      "columns": columns,
      "columnDefs": [{
        "targets": colIndexArray,
        "createdCell": function (td, cellData, rowData, row, col) {
          columnFunction(td, cellData, rowData, row, col);
        }
      }],
      "processing": true, //打开数据加载时的等待效果
      //		       "serverSide": true,//打开后台分页
      "pagingType": "full_numbers", //设置分页控件的模式
      "searching": true, //屏蔽datatales的查询框
      "aLengthMenu": [count], //设置一页展示多少条记录
      "ordering": false,
      "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
      "responsive": true, //自适应
      "fixedHeader": true, //表头固定
      "scrollY": "300px", //dt高度
      "oLanguage": { //对表格国际化
        "sLengthMenu": "每页显示 _MENU_条",
        "sZeroRecords": "没有找到符合条件的数据",
        "sProcessing": "努力加载中",
        "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        "sInfoEmpty": "没有数据",
        "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
        "sSearch": "搜索：",
        "oPaginate": {
          "sFirst": "首页",
          "sPrevious": "前一页",
          "sNext": "后一页",
          "sLast": "尾页"
        }
      },
      initComplete: function () { //列筛选
        var api = this.api();
        api.columns().indexes().flatten().each(function (i) {
          var column = api.column(i);
          var $span = $('<span style="width:60px;" class="addselect">▾</span>').appendTo($(column.header()))
          var select = $('<select style="width:60px;"><option value="">全部</option></select>')
            .appendTo($(column.header()))
            .on('click', function (evt) {
              evt.stopPropagation();
              var val = $.fn.dataTable.util.escapeRegex(
                $(this).val()
              );
              column
                .search(val ? '^' + val + '$' : '', true, false)
                .draw();
            });
          column.data().unique().sort().each(function (d, j) {
            function delHtmlTag(str) {
              return str.replace(/<[^>]+>/g, ""); //去掉html标签
            }

            d = delHtmlTag(d)
            select.append('<option value="' + d + '">' + d + '</option>')
            $span.append(select)
          });
        });
      }
    });
    return table;
  },
  /**
   * Description:链式promise先统一执行ajax请求，根据结果执行成功或失败
   * new Promise(test).then(function (result) {。。。}).catch(function (reason) {。。。});
   * @Author 王荣
   */
  ajaxRequest: function (url, data) {
    if (data === undefined) {
      data = {};
    }
    return new Promise(function (resole, reject) {
      $.ajax({
        url: url,
        type: 'POST',
        data: data,
        dataType: 'json',
        success: function (res) {
          var result = res.result;
          if (!result) {
            layAlert(res.info);
            reject();
            return;
          }
          var data = res.data;
          resole(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          layer.msg(textStatus, {
            icon: 2
          });
          reject({
            textStatus: textStatus,
            errorThrown: errorThrown
          });
        }
      });
    });
  }
};

var inputUtil = {
  //输入负数
  inputNumberFu: function (id) {
    var re = /^[-1-9]+[0-9]*]*$/;
    var str = $("#" + id).val();
    if (!re.test(str)) {
      str = str.substring(0, str.length - 1);
      $("#" + id).val(str);
      return false;
    }
  },
  //	输入1-9数字
  inputNumber: function (id) {
    var re = /^[1-9]+[0-9]*]*$/;
    var str = $("#" + id).val();
    if (!re.test(str)) {
      str = str.substring(0, str.length - 1);
      $("#" + id).val(str);
      return false;
    }
  },
  inputNumberAndWord: function (id) {
    var Regx = /^[A-Za-z0-9]*$/;
    var str = $("#" + id).val();
    if (!Regx.test(str)) {
      layAlert("密码只能包含数字和字母！");
      $("#" + id).val("");
      return false;
    }
  }
}

var arrayUtil = {
  getClassArrayVal: function (className) {
    var arr = new Array();
    $("." + className).each(function () {
      var txt = $(this).val();
      arr.push(txt); //添加到数组中  
    });
    return arr;
  },
  getClassArrayText: function (className) {
    var arr = new Array();
    $("." + className).each(function () {
      var txt = $(this).text();
      arr.push(txt); //添加到数组中  
    });
    return arr;
  }
}

var setUtil = {
  setImg: function () {
    ajaxUtil.ajaxRuquest("workShop/setImg", sendData, function (data) {
      $("img").attr('src', data);
    });
  }
}
var aBindUtil = {
  aOrderBind: function () {
    $(".order").bind("click", function () {
      var orderId = $(this).text();
      location.href = "orderDetail.jsp?orderId=" + orderId;
    });
  }
}


var nameToIdUtil = {
  getProductionDepartment: function (id) {
    ajaxUtil.ajaxRuquest('basicData/getProductionDepartment', null, function (data) {
      var html = "<option value=''>请选择</option>";
      for (var i = 0; i < data.length; i++) {
        html = html + "<option value='" + data[i].id + "'>" + data[i].deptName + "</option>";
      }
      $("#" + id).html("");
      $("#" + id).append(html);
    });
  },
  getWorkShopNameAndId: function (id, deptId) {
    var object = new Object();
    if ("" == deptId) {
      object = null;
    } else {
      object.deptId = deptId;
    }
    ajaxUtil.ajaxRuquest('basicData/getWorkshop', object, function (data) {
      var html = "<option value=''>请选择</option>";
      for (var i = 0; i < data.length; i++) {
        html = html + "<option value='" + data[i].id + "'>" + data[i].workshopName + "</option>";
      }
      $("#" + id).html("");
      $("#" + id).append(html);
    });
  },
  topGetWorkShopNameAndId: function (id, deptId) {
    var object = new Object();
    if ("" == deptId) {
      object = null;
    } else {
      object.deptId = deptId;
    }
    ajaxUtil.ajaxRuquest('../basicData/getWorkshop', object, function (data) {
      var html = "<option value=''>请选择</option>";
      for (var i = 0; i < data.length; i++) {
        html = html + "<option value='" + data[i].id + "'>" + data[i].workshopName + "</option>";
      }
      $("#" + id).html("");
      $("#" + id).append(html);
    });
  },
  topGetProductionDepartment: function (id) {
    ajaxUtil.ajaxRuquest('../basicData/getProductionDepartment', null, function (data) {
      var html = "<option value=''>请选择</option>";
      for (var i = 0; i < data.length; i++) {
        html = html + "<option value='" + data[i].id + "'>" + data[i].deptName + "</option>";
      }
      $("#" + id).html("");
      $("#" + id).append(html);
    });
  },
  getWorkingProcedureNameAndId: function (id, workShopID) {
    ajaxUtil.ajaxRuquest('basicData/getWorkingProcedureByWorkId', {
      'workShopId': workShopID
    }, function (data) {
      var html = "<option value=''>全部</option>";
      for (var i = 0; i < data.length; i++) {
        html = html + "<option value='" + data[i].id + "'>" + data[i].processName + "</option>";
      }
      $("#" + id).html("");
      $("#" + id).append(html);
    });
  },
  getSelectValue: function (name, url, sendData, type) {
    ajaxUtil.ajaxRuquest(url, sendData, function (data) {
      var html = "<option value=''>请选择</option>";
      for (var i = 0; i < data.length; i++) {
        if (type == 0) {
          html += "<option value=" + data[i] + ">" + data[i] + "</option>";
        }
        if (type == 1) {
          html += "<option value=" + data[i].id + ">" + data[i].userName + "</option>";
        }
      }
      $("select[name=" + name + "]").html("");
      $("select[name=" + name + "]").append(html);
    });
  }
}

var requestUtil = {
  getRequest: function () {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  }
}
//封裝alert提示框
function layAlert(str) {
  layer.alert(str, {
    skin: 'layui-layer-lan' //样式类名
      ,
    closeBtn: 1
  });
}
//去除空格
function myTrim(str) {
  if (str == undefined || str == null) {
    return str;
  }
  return str.replace(/(^\s+)|(\s+$)/g, "");
}
